/**
 *文件名称：
 *
 *文件日期：
 *
 *文件描述：
 *
 *文件版本：V1.0
 *
 */

#ifndef MODULE_CONTROL_H
#define MODULE_CONTROL_H

/* 头文件包含区	*/
#include "stdint.h"
#include "myQueue.h"
#include "protocol.h"
#include "include.h"
/* 宏定义区 */
//#define FUN_POINT_BUF_SIZE 64
//#define FUN_POINT_BUF_SIZE 48
//#define QUEUE_SIZE 30
#define FUN_POINT_BUF_SIZE 34
#define QUEUE_SIZE 20

/* 类型声明区 */

typedef enum
{
  NO_ACK,
  ACK
} ACK_STATE;

typedef enum
{
  AT_DEFAULT = 0,
  AT_RESTART,
  AT_ATI,
  AT_ATE0,
  AT_QCCID,
  AT_IMEI,
  AT_CREG,
  AT_CGATT_GET,
  AT_QMTCFG_0,
  AT_QMTCFG_1,
  AT_QMTCFG_2,
  AT_QMTCFG_3,
  AT_QMTOPEN,
  AT_QMTCONN,
  AT_CSQ,
#if (PROJECT_TYPEDEF == CAT1)
  AT_CPIN,
  AT_QMTSUB,
  AT_QENG,
#endif
  AT_CCLK, // 17
  AT_QMTPUB_UPLINK,
#if (PROJECT_TYPEDEF == NB || PROJECT_TYPEDEF == NB_ONENET)
  AT_CFUN0,
  AT_CFUN1,
  AR_NCSEARFCN,
  AT_QCFG,
  AT_NCDP,
  AT_QREGSWT,
  AT_CGDCONT,
  AT_CPSMS,
  AT_NMSTATUS,
  AT_CEDRXS,
  AT_MIPLCONFIG,
  AT_OPEN_LED,
#endif

  AT_QMTPUB_UP_LAMP,
  AT_QMTPUB_1007,
  AT_QMTPUB_2018,
  WAITING,
  DOWMLINK_READ
} AT_CMD;

#if (PROJECT_TYPEDEF == NB || PROJECT_TYPEDEF == NB_ONENET)
typedef enum
{
  NB_UINIT,      /*????*/
  NB_IN_INITCFG, /*?????,???*/
  NB_INITOK,     /*???????*/
  NB_LWM2M_OK,
  NB_FAIL, /*????,????*/
  NB_LWM2M_CFG
} NB_Status_t;
#endif

#pragma pack(1)
typedef struct
{
  uint16_t csq;
  uint8_t imei[20];
  uint8_t iccid[22];
  uint16_t random;    //随机数
  uint8_t QmtSubFlag; //订阅成功标志
  uint8_t mqtt_state;
	uint32_t e1;
} deviceInfo;

typedef struct
{
  uint8_t serial;
  uint32_t cmd_timeout;    /*	命令超时时间	*/
  uint32_t cmd_delay_time; /*  命令延时发送时间 10ms为单位*/
  uint8_t ack_en;          /*  是否需要应答  */
  protocolStruct protocol; /*  协议结构体 */
} my_queue_struct;

typedef struct
{
  uint8_t cmd_num;         /*	当前正在执行的命令序列号	*/
  uint8_t error_code;      /*	错误码对应指令序列号	*/
  uint8_t ack_ok;          /*	指令应答成功	*/
  uint8_t error_code_cnt;  /*	错误重发次数	*/
  uint32_t cmd_tx_time;    /*	指令发送时长	*/
  uint32_t cmd_timeout;    /*	命令超时时间	*/
  uint32_t cmd_delay_time; /*  命令延时发送时间*/
  uint16_t alarm_flag;     /*  告警标志      */
  protocolStruct protocol; /*  协议结构体 */
} global_control_struct;

typedef struct
{
  uint32_t at_time;               // AT指令查询时间
  uint32_t lamp_data_report_time; //路灯数据上报时间
  uint32_t else_data_repota_time; //其他数据，计量数据上报时间
  uint32_t run_data_report_time;  //运行累计时间数据上报时间
  uint32_t first_data;            //刚上线的第一包数据上报时间
  uint32_t ota_timeout;           // ota升级时间超时
} deviceTime;
#pragma pack()

extern deviceTime device_time;
extern global_control_struct global_control;
extern my_queue_struct my_queue_buff[QUEUE_SIZE];
extern deviceInfo device_info;
extern void (*my_tx_fun[FUN_POINT_BUF_SIZE])(void);
extern void (*my_rx_fun[FUN_POINT_BUF_SIZE])(void);
/* 函数声明区 */
uint16_t do_crc(uint16_t reg_init, uint8_t *data, uint16_t length);
static void my_cmd_retx(QUEUE_T *pQueue);
static void my_cmd_rxdeal(void);
void my_queue_init(void);
void my_Com_Task(void);
void my_cmd_init(void);
void AT_RxCallback(void);
void my_cmd_runing(void);
void my_cmd_deal(uint8_t cmd, void (*tx_fun)(void), void (*rx_fun)(void), uint32_t delay_time, uint32_t timeout, ACK_STATE ack_flag);
void data_report(void);
uint8_t crc_check(uint8_t *in_buf, uint16_t in_buf_len);
uint16_t crc16(uint8_t *Pushdata, uint16_t length);

#endif
